package com.lk.service.impl;

import com.lk.mapper.BrandMapper;
import com.lk.pojo.Brand;
import com.lk.pojo.PageBean;
import com.lk.service.BrandService;
import com.lk.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService{

    //创建SqlSessionFactory工厂
    SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Brand> selectAll() {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        List<Brand> brands = mapper.selectAll();
        //释放资源
        sqlSession.close();

        return brands;
    }

    @Override
    public void add(Brand brand) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.add(brand);
        //提交
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public void updateById(Brand brand) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.updateById(brand);
        //提交
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }


    @Override
    public void deleteById(int id) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.deleteById(id);
        //提交
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }


    @Override
    public void deleteByIds(int[] ids) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.deleteByIds(ids);
        //提交
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //计算开始索引
        int begin=(currentPage - 1)*pageSize;
        //获取每页查询条目数
        int size=pageSize;

        //查询当前页数据
        List<Brand> list = mapper.selectByPage(begin, size);

        //查询总记录数
        int totalCount = mapper.selectTotalCount();

        //封装对象
        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(list);

        //释放资源
        sqlSession.close();

        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //计算开始索引
        int begin=(currentPage - 1)*pageSize;
        //获取每页查询条目数
        int size=pageSize;

        //处理查询条件，模糊表达式
        String brandName=brand.getBrandName();
        if(brandName != null &&brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
        String companyName=brand.getCompanyName();
        if(companyName != null &&companyName.length()>0){
            brand.setCompanyName("%"+companyName+"%");
        }
        //查询当前页数据
        List<Brand> list = mapper.selectByPageAndCondition(begin, size,brand);

        //查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(brand);

        //封装对象
        PageBean<Brand> pageBean=new PageBean<>();
        pageBean.setTotalCount(totalCount);
        pageBean.setRows(list);

        //释放资源
        sqlSession.close();

        return pageBean;
    }

}
